package org.spiderflow.common.controller;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.spiderflow.common.model.JsonBean;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;

public abstract class CURDController<S extends ServiceImpl<M, T>, M extends BaseMapper<T>, T> {

    protected final S service;

    protected CURDController(S service) {
        this.service = service;
    }

    @GetMapping("/get/{id}")
    public JsonBean get(@PathVariable String id) {
        return new JsonBean(service.getById(id));
    }

    @DeleteMapping("/delete/{id}")
    public JsonBean delete(@PathVariable String id) {
        return new JsonBean(service.removeById(id));
    }

    @PostMapping("/save")
    public JsonBean save(T t) {
        return new JsonBean(service.saveOrUpdate(t));
    }

    @PutMapping("/update")
    public JsonBean update(T t) {
        return new JsonBean(service.saveOrUpdate(t));
    }

}